package com.syh.jn.scm.service.impl.downstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.base.DownBankAccount;
import com.syh.jn.scm.domain.downstream.repository.base.DownBankAccountRepository;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountMto;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountSto;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountVo;
import com.syh.jn.scm.service.api.downstream.base.DownBankAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 银行账户管理
 */
@Service
@Transactional
public class DownBankAccountServiceImpl extends AbstractServiceImpl<DownBankAccountSto, DownBankAccountMto, DownBankAccountVo, DownBankAccount, Long> implements DownBankAccountService {
  @Autowired
  private DownBankAccountRepository downBankAccountRepository;

  @Override
  protected AggregateRepository<DownBankAccount, Long> getAggregateRepository() {
    return downBankAccountRepository;
  }

  /**
   * 保存银行账户
   * @param sto
   * @return
   */
  @Override
  public Long save(DownBankAccountSto sto) {
    DownBankAccount downBankAccount = DownBankAccount.createBusiness(sto);
    return downBankAccountRepository.add(downBankAccount);
  }

  /**
   * 修改银行账户
   * @param mto
   */
  @Override
  public void update(DownBankAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    //根据id查找对应的银行账户并判空
    DownBankAccount downBankAccount = this.getDownBankAccountById(mto.getId());
    downBankAccount.modify(mto);
    downBankAccountRepository.update(downBankAccount);
  }

  /**
   * 删除银行账户
   * @param id
   */
  @Override
  public void delete(Long id) {
    //根据id查找对应的银行账户并判空
    DownBankAccount downBankAccount = this.getDownBankAccountById(id);

    downBankAccount.destroy();
    downBankAccountRepository.removeById(id);
  }

  /**
   * 启用
   */
  @Override
  public void enabled(Long id) {
    //根据id查找对应的银行账户并判空
    DownBankAccount downBankAccount = this.getDownBankAccountById(id);

    downBankAccount.enabled();
    downBankAccountRepository.update(downBankAccount);
  }

  /**
   * 停用
   */
  @Override
  public void disabled(Long id) {
    //根据id查找对应的银行账户并判空
    DownBankAccount downBankAccount = this.getDownBankAccountById(id);

    downBankAccount.disabled();
    downBankAccountRepository.update(downBankAccount);
  }

  /**
   * 根据id查找对应的银行账户并判空
   * @param id
   * @return
   */
  private DownBankAccount getDownBankAccountById(Long id){
    ExtAssert.notNull(id, "id为空");

    DownBankAccount downBankAccount = downBankAccountRepository.getById(id);
    ExtAssert.notNull(downBankAccount, "对象为空");

    return downBankAccount;
  }
}
